<template>
  <div class="category-container">
    <div class="category-nav">
      <div class="nav-header">全部商品分类</div>
      <ul>
        <li 
          v-for="category in categories" 
          :key="category.id"
          @mouseenter="activeCategory = category"
          @mouseleave="activeCategory = null"
          :class="{ 'active': activeCategory && activeCategory.id === category.id }"
        >
          <span>{{ category.name }}</span>
          <span class="arrow">></span>
        </li>
      </ul>
    </div>
    <transition name="fade">
      <div 
        v-if="activeCategory"
        class="mega-menu"
        @mouseenter="activeCategory = activeCategory" 
        @mouseleave="activeCategory = null"
      >
        <div v-for="group in activeCategory.groups" :key="group.title" class="menu-group">
          <h3>{{ group.title }}</h3>
          <ul>
            <li v-for="item in group.items" :key="item.id">
              <a href="#">{{ item.name }}</a>
            </li>
          </ul>
        </div>
        
      </div>
    </transition>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const activeCategory = ref(null);

const categories = ref([
  {
    id: 1, name: '手机 / 运营商 / 数码',
    groups: [
      { title: '手机通讯', items: [{id: 101, name: '手机'}, {id: 102, name: '游戏手机'}, {id: 103, name: '5G手机'}] },
      { title: '运营商', items: [{id: 104, name: '合约机'}, {id: 105, name: '选好卡'}, {id: 106, name: '办套餐'}] },
      { title: '数码配件', items: [{id: 107, name: '手机壳'}, {id: 108, name: '贴膜'}, {id: 109, name: '充电器'}] }
    ]
  },
  {
    id: 2, name: '电脑 / 办公 / 家电',
    groups: [
      { title: '电脑整机', items: [{id: 201, name: '笔记本'}, {id: 202, name: '台式机'}, {id: 203, name: '一体机'}] },
      { title: '电脑配件', items: [{id: 204, name: 'CPU'}, {id: 205, name: '显卡'}, {id: 206, name: '内存'}] },
      { title: '家用电器', items: [{id: 207, name: '冰箱'}, {id: 208, name: '洗衣机'}, {id: 209, name: '空调'}] }
    ]
  },
  {
    id: 3, name: '家居 / 家具 / 家装',
    groups: [
      { title: '厨具', items: [{id: 301, name: '烹饪锅具'}, {id: 302, name: '刀剪菜板'}] },
      { title: '家纺', items: [{id: 303, name: '被子'}, {id: 304, name: '枕头'}, {id: 305, name: '四件套'}] },
      { title: '灯具', items: [{id: 306, name: '台灯'}, {id: 307, name: '吸顶灯'}] }
    ]
  }
]);
</script>

<style scoped>
.category-container {
  position: relative;
  width: 220px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

.category-nav {
  border: 1px solid #e0e0e0;
  background-color: #fff;
}

.nav-header {
  background-color: #c81623;
  color: white;
  padding: 12px 15px;
  font-size: 16px;
  font-weight: bold;
}

.category-nav ul {
  list-style: none;
  padding: 5px 0;
  margin: 0;
}

.category-nav li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 15px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.2s ease;
}

.category-nav li.active, .category-nav li:hover {
  background-color: #f5f5f5;
}

.arrow {
  color: #999;
  font-size: 12px;
}

.mega-menu {
  position: absolute;
  top: 0;
  left: 100%;
  width: 700px;
  min-height: 100%;
  background-color: #fff;
  border: 1px solid #e0e0e0;
  box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
  z-index: 100;
  padding: 20px;
  display: flex;
  flex-wrap: wrap;
}

.menu-group {
  width: 50%;
  padding: 0 15px;
  margin-bottom: 20px;
}

.menu-group h3 {
  font-size: 14px;
  font-weight: bold;
  border-bottom: 1px solid #eee;
  padding-bottom: 5px;
  margin: 0 0 10px 0;
}

.menu-group ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.menu-group li {
  padding: 5px 0;
}

.menu-group a {
  text-decoration: none;
  color: #333;
  font-size: 13px;
}

.menu-group a:hover {
  color: #c81623;
}

.promo-section {
  width: 100%;
  margin-top: 10px;
}

.promo-section img {
  max-width: 100%;
  height: auto;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from, .fade-leave-to {
  opacity: 0;
}
</style>
